<?php
/**
 * Created by PhpStorm.
 * User: zhangchao
 * Date: 15/12/11
 * Time: 20:37
 */

namespace backend\controllers;


use app\models\AdImg;
use app\models\Contactor;
use app\models\ContactorCat;
use app\models\Customer;
use app\models\CustomerCat;
use app\models\Supply;
use common\models\Division;
use yii\data\ActiveDataProvider;
use yii\db\ActiveRecord;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\web\Controller;

class HomeController extends Controller
{
    //每页显示的数量
    const PAGECOUNT = 20;
    public $enableCsrfValidation = false;
    public $layout = 'front';

    function actionIndex()
    {
        $adModel = AdImg::getAd();
        return $this->render('index' , [
            'adModel' => $adModel,
        ]);
    }

    public function actionView()
    {

        $page = intval(\Yii::$app->request->get('page'));
        $offset  = ($page - 1) * self::PAGECOUNT;
        $ishot = intval(\Yii::$app->request->get('ishot'));

       if($ishot == 1){
            $query = Contactor::find()->where(['is_hot' => 1])->limit(self::PAGECOUNT);
        }else{
            $cat = intval(\Yii::$app->request->get('cat'));
           $city = intval(\Yii::$app->request->get('city'));
            if($cat != 0){
                $query = ContactorCat::find()->limit(self::PAGECOUNT);
                $query->select('zs_contactor_cat.* , c.*');
                $query->leftJoin(['c' => 'zs_contactor'] , 'c.id = zs_contactor_cat.contactor_id');
                $query->where(['cat_id' => $cat]);
            }else{
                $query = Contactor::find()->limit(self::PAGECOUNT);
            }
           if($city != 0){
                $query->andWhere(['city_id' => $city]);
            }
        }

        $keyword =   Html::encode(\Yii::$app->request->get('keyword'));
        if($keyword && $keyword != ''){
            $query->andFilterWhere(['like' , 'company_name' , $keyword])
                ->orFilterWhere(['like' , 'mobile' , $keyword]);
        }
        $query->orderBy('id desc');


        $page = $this->createPageUrl('/home/view?', $page);

        $data = $query->asArray()->offset($offset)->all();

        return $this->render('view' , [
            'data'  => $data,
            'cat'   => empty($cat) ? 0 : $cat,
            'city'  => empty($city) ? 0 : $city,
            'ishot' => empty($ishot) ? 0 : $ishot,
            'page' => $page,
        ]);

    }

    public function createPageUrl($baseurl,$page = 0)
    {
        $page = $page == 0 ? 1 : $page;
        $next_page = $page + 1;
        $pre_page = $page - 1;
        $url = parse_url(\Yii::$app->request->url);
        $params = [];
        if(isset($url['query'])){
            parse_str($url['query'] , $params);
            $param = isset($params[1]) ? $params[1] : $params;
        }else{
            $param = $params;
        }

        $data = [];

        $param['page'] = $next_page;


        $data['next'] = $baseurl.http_build_query($param);

        $param['page'] = $pre_page;
        if($pre_page <= 0){
            unset($param['page']);
        }

        $data['pre']  = $baseurl.http_build_query($param);
        return $data;

//        var_dump($data);die;
    }

    public function actionSubmit()
    {

        $contactorModel = new Contactor();
        if($contactorModel->load(\Yii::$app->request->post())){
            $contactorModel->cat = explode(',',\Yii::$app->request->post('cat'));
            if($contactorModel->save() && $contactorModel->saveCat()){
                echo json_encode(['success' => false , 'msg' => '创建失败']);die;
            }else{
                echo json_encode(['success' => true , 'msg' => '创建成功']);die;

            }
        }
        $adModel = AdImg::getAd(2);
        return $this->render('submit' ,[
            'adModel' => $adModel,
        ]);
    }

    public function actionCustomer()
    {

        $customer = Customer::find()->asArray()->all();
        $adModel = AdImg::getAd(3);
        return $this->render('customer_list', [
            'customer' => $customer,
            'ad' => $adModel,
        ]);
    }

    public function actionCity()
    {
        if(!\Yii::$app->request->isAjax){
            echo json_encode([]);die;
        }
        $city_arr = Division::getProvinceCity();
        echo json_encode($city_arr);die;

    }

    public function actionCat()
    {
        if(!\Yii::$app->request->isAjax){
            echo json_encode([]);die;
        }
        $cat_arr = CustomerCat::getCatArr();
        echo json_encode($cat_arr);die;
    }

    public function actionCheckMobile()
    {
        $mobile = intval(\Yii::$app->request->get('tel'));
        $contactorModel = Contactor::findOne(['mobile' => $mobile]);
        if(!$contactorModel){
            echo json_encode(['success' => true]);
        }else{
            echo json_encode(['success' => false , 'msg' => '手机号已经存在，是否确认提交？']);
        }
        die;
    }

    public function actionInfo()
    {
        $page = intval(\Yii::$app->request->get('page'));
        $offset  = ($page - 1) * self::PAGECOUNT;



        $type = intval(\Yii::$app->request->get('type'));
        if($type == 0){
            $type = 1;
        }

        $supplies = Supply::find()->where(['type' => $type])->limit(10)->offset($offset)->asArray()->all();

        $adModel = AdImg::getAd(4);
        $pageArr = $this->createPageUrl('/home/info?', $page);
        return $this->render('list' , [
            'supplies' => $supplies,
            'type' => $type,
            'adModel' => $adModel,
            'page' => $pageArr,
        ]);
    }

    public function actionContent()
    {
        $sid = intval(\Yii::$app->request->get('sid'));
        if($sid == 0){
            return false;
        }
        $act = \Yii::$app->request->get('act');
        if($act == 'next'){
            $model = Supply::findOne(['>' ,'id' ,$sid]);
        }else{
            $model = Supply::findOne(['id' => $sid]);
        }

        $adModel = AdImg::getAd(5);
        return $this->render('content' , [
            'model' => $model,
            'adModel' => $adModel,
        ]);

    }



}